package io.renren.controller;

import io.renren.entity.ConfigModel;
import io.renren.entity.DataSource;
import io.renren.utils.DBHelper;
import io.renren.utils.DynamicDataSource;
import io.renren.utils.R;
import io.renren.utils.SettingConfig;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.ValidationUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @className: ConfController
 * @program: renren-generator
 * @description: 配置
 * @author: wzp12
 * @create: 2019-06-03 10:42
 **/
@RestController
@RequestMapping("sys/conf")
public class ConfController {
    //<editor-fold desc="配置页面">

    @GetMapping("/confget")
    public R confGet() {

        Map<String, String> conf = new HashMap<String, String>() {{
            put("mainPath", SettingConfig.mainPath);
            put("packageName", SettingConfig.packageName);
            put("moduleName", SettingConfig.moduleName);
            put("author", SettingConfig.author);
            put("email", SettingConfig.email);
            put("tablePrefix", SettingConfig.tablePrefix);
            put("suffix", SettingConfig.suffix);
        }};

        return R.ok().put("conf", conf);
    }

    @PostMapping("/conf")
    public R conf(@RequestBody ConfigModel model) {
        SettingConfig.mainPath = model.getMainPath();
        SettingConfig.packageName = model.getPackageName();
        SettingConfig.moduleName = model.getModuleName();
        SettingConfig.author = model.getAuthor();
        SettingConfig.email = model.getEmail();
        SettingConfig.tablePrefix = model.getTablePrefix();
        SettingConfig.suffix = model.getSuffix();
        return R.ok();
    }


    //<editor-fold desc="数据库设置">


    @GetMapping("/dbconfget")
    public R dataSourceConfGet() {

        Map<String, String> conf = new HashMap<String, String>() {{
            put("driverClass", SettingConfig.defaultSource.getDriverClass());
            put("jdbcUrl", SettingConfig.defaultSource.getJdbcUrl());
            put("user", SettingConfig.defaultSource.getUser());
            put("password", SettingConfig.defaultSource.getPassword());
        }};

        return R.ok().put("conf", conf);
    }

    @PostMapping("/dbconf")
    public R dataSourceConf(@RequestBody ConfigModel model) {
        DataSource dataSource = new DataSource();

        if (StringUtils.isBlank(model.getJdbcUrl())) {
            return R.error(500, "连接字符串不能为空");
        }
        if (StringUtils.isBlank(model.getDriverClass())) {
            return R.error(500, "驱动不能为空");
        }
        if (StringUtils.isBlank(model.getUser())) {
            return R.error(500, "用户不能为空");
        }
        if (StringUtils.isBlank(model.getPassword())) {
            return R.error(500, "密码不能为空");
        }

        dataSource.setDriverClass(model.getDriverClass());
        dataSource.setJdbcUrl(model.getJdbcUrl());
        dataSource.setUser(model.getUser());
        dataSource.setPassword(model.getPassword());

        try {
            if (DBHelper.testConnection(dataSource)) {
                SettingConfig.defaultSource = dataSource;
                SettingConfig.sourceSet.add(dataSource);

                return R.ok();
            } else {
                return R.error(504, "连接失败");
            }
        } catch (Exception e) {
            return R.error(500, "连接失败");
        }
    }


    //</editor-fold>
}
